package com.freaky.jx3.mjb;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MJBActivity extends Activity implements OnGestureListener,
		OnTouchListener {
	/** Called when the activity is first created. */

	private Button btnGenerate;
	private EditText edtTwoVsTwo;
	private EditText edtThreeVsThree;
	private EditText edtFiveVsFive;
	private TextView txtResult;

	GestureDetector mGestureDetector;

	private EditText edtGetMjbNum;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btnGenerate = (Button) findViewById(R.id.btnGenerate);
		edtTwoVsTwo = (EditText) findViewById(R.id.edtTwoVsTwo);
		edtThreeVsThree = (EditText) findViewById(R.id.edtThreeVsThree);
		edtFiveVsFive = (EditText) findViewById(R.id.edtFiveVsFive);
		txtResult = (TextView) findViewById(R.id.txtResult);

		// 初始化赋值
		edtTwoVsTwo.setText(BaseConstant.num2v2);
		edtThreeVsThree.setText(BaseConstant.num3v3);
		edtFiveVsFive.setText(BaseConstant.num5v5);
		txtResult.setText(BaseConstant.result);

		btnGenerate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 名剑币计算
				StringBuilder strResult = new StringBuilder();
				if (!edtTwoVsTwo.getText().toString().equals("")) {
					strResult.append("2v2得分为：");
					int num2v2 = Integer.valueOf(edtTwoVsTwo.getText()
							.toString());
					BaseConstant.num2v2 = "" + num2v2;
					strResult.append(BaseConstant.mjbCalculate(num2v2,
							BaseConstant.modulus2v2));
					strResult.append("\n");
					edtTwoVsTwo.clearFocus();
				}
				if (!edtThreeVsThree.getText().toString().equals("")) {
					strResult.append("3v3得分为：");
					int num3v3 = Integer.valueOf(edtThreeVsThree.getText()
							.toString());
					BaseConstant.num3v3 = "" + num3v3;
					strResult.append(BaseConstant.mjbCalculate(num3v3,
							BaseConstant.modulus3v3));
					strResult.append("\n");
					edtThreeVsThree.clearFocus();
				}
				if (!edtFiveVsFive.getText().toString().equals("")) {
					strResult.append("5v5得分为：");
					int num5v5 = Integer.valueOf(edtFiveVsFive.getText()
							.toString());
					BaseConstant.num5v5 = "" + num5v5;
					strResult.append(BaseConstant.mjbCalculate(num5v5,
							BaseConstant.modulus5v5));
					strResult.append("\n");
					edtFiveVsFive.clearFocus();
				}
				txtResult.setText(strResult.toString());

				// 赋值保存
				BaseConstant.result = strResult.toString();
				// 关闭输入法
				// edtTwoVsTwo.setInputType(InputType.TYPE_NULL);
				InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				m.hideSoftInputFromWindow(edtTwoVsTwo.getWindowToken(), 0);
				/*
				 * boolean isOpen=m.isActive(); if (isOpen) {
				 * m.toggleSoftInput(1, InputMethodManager.HIDE_NOT_ALWAYS); }
				 */

			}
		});
		edtTwoVsTwo.addTextChangedListener(new EditTextMaxLengthWatcher(4,
				edtTwoVsTwo));
		edtThreeVsThree.addTextChangedListener(new EditTextMaxLengthWatcher(4,
				edtThreeVsThree));
		edtFiveVsFive.addTextChangedListener(new EditTextMaxLengthWatcher(4,
				edtFiveVsFive));
		edtGetMjbNum=new EditText(this);
		edtGetMjbNum.setInputType(InputType.TYPE_CLASS_NUMBER);
		
		txtResult.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Builder alertDialog = new Builder(MJBActivity.this)
						.setTitle("请输入当前名剑币数");
				alertDialog.setView(edtGetMjbNum);

				alertDialog.setPositiveButton("确定",
						new android.content.DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								setResult(RESULT_OK);
								Toast.makeText(
										MJBActivity.this,
										"下周共有名剑币："
												+ Integer.valueOf(edtGetMjbNum
														.getText().toString()),
										Toast.LENGTH_SHORT).show();

							}

						});
				alertDialog.show();

			}
		});

		// 界面切换
		mGestureDetector = new GestureDetector((OnGestureListener) this);
		LinearLayout linMain = (LinearLayout) findViewById(R.id.linMain);
		linMain.setOnTouchListener(this);
		linMain.setLongClickable(true);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > 50 && Math.abs(velocityX) > 0) {
			// 画面切换
			Intent intent = new Intent();
			intent.setClass(MJBActivity.this, MJBPegging.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			MJBActivity.this.finish();
			startActivity(intent);
			// Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
		} else if (e2.getX() - e1.getX() > 50 && Math.abs(velocityX) > 0) {
			// Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();

		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	// 返回键监听
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			System.exit(0);
			return false;
		}
		return false;
	}

}
